<?php

namespace app\admin\controller;

use app\admin\model\QuestionCatModel;
use app\admin\model\QuestionPhotoModel;
use think\Db;
 
class QuestionPhoto extends Base
{

    /**
     * [index 轮播列表]
     * @return [type] [description]
     * @author felix
     */
    public function index(){

        $key = input('id');
        $map = [];
        //$map['is_show'] = 1;
        if($key&&$key!==""){
            $map['question_id'] = $key;
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('question_photo')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $goods_cat = new QuestionPhotoModel();
        //dump($map);
        $lists = $goods_cat->getDataByWhere($map, $Nowpage, $limits);
        //dump($lists);
        /*$type = ['1'=>'图文', '2'=>'外链', '3'=>'正常'];
        // dump($lists);
        foreach($lists as $k=>$v)
        {
            $lists[$k]['type'] = $type[$v['type']];
            //$lists[$k]['is_show'] = $v['is_show'] == 1 ? '显示':'隐藏' ;
        }*/
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('question_id', $key);
        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionPhotoAdd()
    {
        $key = input('question_id');
        if(request()->isAjax()){

            $param = input('post.');
            //dump($param);die;
            $goods_cat = new QuestionPhotoModel();
            $flag = $goods_cat->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $this->assign('question_id', $key);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionPhotoEdit()
    {
        $goods_cat = new QuestionPhotoModel();
        if(request()->isAjax()){

            $param = input('post.');
            $flag = $goods_cat->updateData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $key = input('question_id');
        $this->assign('question_id', $key);
        $this->assign('photo',$goods_cat->getOneData($id));
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionPhotoDel()
    {
        $id = input('param.id');
        $goods_cat = new QuestionPhotoModel();
        $flag = $goods_cat->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * @return [type] [description]
     * @author felix
     */
    public function question_state()
    {
        $id = input('param.id');
        $status = Db::name('question_photo')->where('id',$id)->value('is_show');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('question_photo')->where('id',$id)->setField(['is_show'=>-1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已隐藏']);
        }
        else
        {
            $flag = Db::name('question_photo')->where('id',$id)->setField(['is_show'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已显示']);
        }

    }


}